import { createSlice, PayloadAction } from '@reduxjs/toolkit'

interface Course {
  id: string
  title: string
  cover?: string
  price: number
  level: string
  studentCount: number
}

interface CourseState {
  courses: Course[]
  currentCourse: any | null
  loading: boolean
  total: number
}

const initialState: CourseState = {
  courses: [],
  currentCourse: null,
  loading: false,
  total: 0,
}

const courseSlice = createSlice({
  name: 'course',
  initialState,
  reducers: {
    setCourses: (state, action: PayloadAction<{ courses: Course[]; total: number }>) => {
      state.courses = action.payload.courses
      state.total = action.payload.total
    },
    setCurrentCourse: (state, action: PayloadAction<any>) => {
      state.currentCourse = action.payload
    },
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload
    },
  },
})

export const { setCourses, setCurrentCourse, setLoading } = courseSlice.actions
export default courseSlice.reducer
